package org.light.shiroauth;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.exception.ValidateException;
import org.light.generator.Generator;
import org.light.utils.WriteableUtil;

public class SimpleFormAuthenticationFilterGenerator extends Generator{
	protected Domain sampleDomain;
	
	public Domain getSampleDomain() {
		return sampleDomain;
	}

	public void setSampleDomain(Domain sampleDomain) {
		this.sampleDomain = sampleDomain;
	}

	public SimpleFormAuthenticationFilterGenerator(Domain sampleDomain){
		super();
		this.setPackageToken(sampleDomain.getPackageToken());
		this.sampleDomain = sampleDomain;
		super.fileName = "SimpleFormAuthenticationFilter.java";
	}
	
	@Override
	public StatementList generateStatementList() throws ValidateException {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(500L,"package "+this.getPackageToken()+".shiro;"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import java.io.PrintWriter;"));
		sList.add(new Statement(4000L,0,"import java.util.HashMap;"));
		sList.add(new Statement(5000L,0,"import java.util.Map;"));
		if ("boot3".equalsIgnoreCase(this.sampleDomain.getTechnicalStack())||"jpa".equalsIgnoreCase(this.sampleDomain.getTechnicalStack())) {
			sList.add(new Statement(6000L,0,"import jakarta.servlet.ServletRequest;"));
			sList.add(new Statement(7000L,0,"import jakarta.servlet.ServletResponse;"));
			sList.add(new Statement(8000L,0,"import jakarta.servlet.http.HttpServletRequest;"));
			sList.add(new Statement(9000L,0,"import jakarta.servlet.http.HttpServletResponse;"));
		} else {
			sList.add(new Statement(6000L,0,"import javax.servlet.ServletRequest;"));
			sList.add(new Statement(7000L,0,"import javax.servlet.ServletResponse;"));
			sList.add(new Statement(8000L,0,"import javax.servlet.http.HttpServletRequest;"));
			sList.add(new Statement(9000L,0,"import javax.servlet.http.HttpServletResponse;"));
		}
		sList.add(new Statement(10000L,0,"import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;"));
		sList.add(new Statement(11000L,0,"import org.apache.shiro.web.util.WebUtils;"));
		sList.add(new Statement(12000L,0,"import org.slf4j.Logger;"));
		sList.add(new Statement(13000L,0,"import org.slf4j.LoggerFactory;"));
		sList.add(new Statement(14000L,0,"import com.alibaba.fastjson.JSON;"));
		sList.add(new Statement(15000L,0,""));
		sList.add(new Statement(16000L,0,"public class SimpleFormAuthenticationFilter extends FormAuthenticationFilter {"));
		sList.add(new Statement(17000L,0,""));
		sList.add(new Statement(18000L,1,"private static final Logger log = LoggerFactory.getLogger(SimpleFormAuthenticationFilter.class);"));
		sList.add(new Statement(19000L,1,""));
		sList.add(new Statement(20000L,1,"public SimpleFormAuthenticationFilter() {"));
		sList.add(new Statement(21000L,2,"super();"));
		sList.add(new Statement(22000L,1,"}"));
		sList.add(new Statement(23000L,1,""));
		sList.add(new Statement(24000L,1,"@Override"));
		sList.add(new Statement(25000L,1,"public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {"));
		sList.add(new Statement(26000L,2,"//Always return true if the request's method is OPTIONS"));
		sList.add(new Statement(27000L,2,"if (request instanceof HttpServletRequest) {"));
		sList.add(new Statement(28000L,3,"if (((HttpServletRequest) request).getMethod().toUpperCase().equals(\"OPTIONS\")) {"));
		sList.add(new Statement(29000L,4,"return true;"));
		sList.add(new Statement(30000L,3,"}"));
		sList.add(new Statement(31000L,2,"}"));
		sList.add(new Statement(32000L,2,"return super.isAccessAllowed(request, response, mappedValue);"));
		sList.add(new Statement(33000L,1,"}"));
		sList.add(new Statement(34000L,0,""));
		sList.add(new Statement(35000L,1,"@Override"));
		sList.add(new Statement(36000L,1,"protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {"));
		sList.add(new Statement(37000L,3,"HttpServletResponse res = (HttpServletResponse)response;"));
		sList.add(new Statement(38000L,3,"res.setHeader(\"Access-Control-Allow-Origin\", \"true\");"));
		sList.add(new Statement(39000L,3,"res.setContentType(\"application/json; charset=utf-8\");"));
		sList.add(new Statement(40000L,3,"res.setStatus(HttpServletResponse.SC_OK);"));
		sList.add(new Statement(41000L,3,"PrintWriter writer = res.getWriter();"));
		sList.add(new Statement(42000L,3,"Map<String, Object> map= new HashMap<>();"));
		sList.add(new Statement(43000L,3,"map.put(\"success\", false);"));
		sList.add(new Statement(44000L,3,"map.put(\"noAuth\", true);"));
		if ("english".equalsIgnoreCase(this.sampleDomain.getLanguage())) {
			sList.add(new Statement(45000L,3,"map.put(\"error\", \"No privilege.\");"));
		}else {
			sList.add(new Statement(45000L,3,"map.put(\"error\", \"无权限\");"));
		}
		sList.add(new Statement(46000L,3,"writer.write(JSON.toJSONString(map));"));
		sList.add(new Statement(47000L,3,"writer.close();"));
		sList.add(new Statement(48000L,3,"//return false 拦截， true 放行"));
		sList.add(new Statement(49000L,3,"return false;"));
		sList.add(new Statement(50000L,1,"}"));
		sList.add(new Statement(51000L,0,""));
		sList.add(new Statement(52000L,1,"/*"));
		sList.add(new Statement(53000L,1,"* 判断ajax请求"));
		sList.add(new Statement(54000L,1,"* @param request"));
		sList.add(new Statement(55000L,1,"* @return"));
		sList.add(new Statement(56000L,1,"*/"));
		sList.add(new Statement(57000L,1,"boolean isAjax(HttpServletRequest request){"));
		sList.add(new Statement(58000L,2,"return  (request.getHeader(\"X-Requested-With\") != null  && \"XMLHttpRequest\".equals( request.getHeader(\"X-Requested-With\").toString())   ) ;"));
		sList.add(new Statement(59000L,1,"}"));
		sList.add(new Statement(60000L,0,""));
		sList.add(new Statement(61000L,0,"}"));
		
		return WriteableUtil.merge(sList);
	}

}
